<?php
require_once("config.php");
require_once("Info.php");
require_once("Database.php");
require_once("Lineup.php");
require_once("Cache.php");
$lockfile = "./cache/proc.lck";

set_time_limit(9999);
ini_set("mysql.connect_timeout", 9999);

$db = Database::getInstance();
$query = $db->query("SELECT * FROM cachequeue ORDER BY type DESC");

try {
    while($row = mysql_fetch_array($query)) {
        if($row['type'] == "artist.getsimilar") {
            $file = "./cache/artist/similar/".$row['name'].".xml";
            if(!file_exists($file)) {
                $xmlUrl = "http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=".$row['name']."&api_key=519681edb551a3b5177bb67352e16136&limit=150";
                echo "caching: ".$xmlUrl;
            }
           sleep(1); //prevent hammering the poor last.fm server
        } elseif($row['type'] == "geo.getevents") {
            Lineup::getEvents($row['name'], true);
        } else {
           trigger_error("\$row['type'] not set"); die;
        }
        if(!empty($xmlUrl)) {
            $xml = file_get_contents($xmlUrl);
            $fp = @fopen($file, "w");
            if($fp) {
                $result = fwrite($fp, $xml);
                fclose($fp);
            }
        }
        $db->query("DELETE FROM cachequeue WHERE name='".$row['name']." ' AND type='".$row['type']."'");
    }
} catch(Exception $e) {
    Info::dump($e->getMessage());
    $c = Cache::getInstance();
    $out = $c->startCaching();
    echo "Starting new cacheproc; ".$out; 
}
unlink($lockfile);

?>